package com.JingYan.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.DecimalFormat;

/**
 * @author DongJiaQi
 * @create 2022/7/27 10:59
 * @Description: 将 Double 转为三位小数
 * 可以将json 返回的小数改为指定位数
 * 用法 :  在属性上注解
 *     @JsonSerialize(using = DoubleThreeDecimalSerialize.class)
 */
public class DoubleThreeDecimalSerialize extends JsonSerializer<Double> {
    private DecimalFormat df = new DecimalFormat("#.###");
    @Override
    public void serialize(Double value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if(value != null) {
            gen.writeString(df.format(value));
        }

    }
}
